package com.frikinet.lifeguard.gui;

import static ch.lambdaj.Lambda.convert;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.TreeSet;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.lambdaj.function.convert.Converter;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.events.YearMonthSelectedEvent;
import com.frikinet.lifeguard.events.YearSelectedListener;
import com.frikinet.lifeguard.model.Post;
import com.frikinet.lifeguard.model.User;

public class YearNavigation extends JPanel implements YearSelectedListener
{
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory.getLogger(PostViewer.class);
	
	private Font clickFont, currentFont;
	
	private JLabel previousYear, nextYear;
	private JLabel currentYear;
	
	private int year;
	private User user;
	
	public YearNavigation()
	{
		super(new GridLayout(1, 3));
		
		App.events.addYearSelectedListener(this);
		
		Font defaultFont = UIManager.getDefaults().getFont("Label.font");
		clickFont = new Font(defaultFont.getName(), Font.BOLD, defaultFont.getSize()+2);
		currentFont = new Font(defaultFont.getName(), defaultFont.getStyle(), defaultFont.getSize()+2);
		
		buildGUI();
	}
	
	private void buildGUI()
	{
		setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		
		JPanel west = new JPanel(new FlowLayout(FlowLayout.LEFT));
		previousYear = setupClickLabel(YearNavigationType.PREVIOUS);
		west.add(previousYear);
		add(west);
		
		JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
		currentYear = new JLabel(" ");
		currentYear.setFont(currentFont);
		center.add(currentYear);
		add(center);
		
		JPanel east = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		nextYear = setupClickLabel(YearNavigationType.NEXT);
		east.add(nextYear);
		add(east);
	}
	
	private JLabel setupClickLabel(YearNavigationType type)
	{
		JLabel label = new JLabel(" ");
		label.setFont(clickFont);
		label.setForeground(Color.blue);
		label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label.addMouseListener(new YearNavigationListener(type));
		return label;
	}
	
	@Override
	public void yearSelected(YearMonthSelectedEvent event)
	{
		List<Integer> years = new ArrayList<Integer>(Arrays.asList(new TreeSet<Integer>(convert(event.getUser().getPosts(), new Converter<Post, Integer>() {
			@Override
			public Integer convert(Post post)
			{
				Calendar c = Calendar.getInstance();
				c.setTime(post.getDate());
				return c.get(Calendar.YEAR);
			}
		})).toArray(new Integer[]{})));
		
		
		year = event.getYear();
		user = event.getUser();
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		
		currentYear.setText(App.i18n.translate("comp.year.navigation.year.current", calendar.getTime()));
		
		int index = years.indexOf(year);
		
		if(index > 0)
		{
			int previous = years.get(index-1);
			calendar.set(Calendar.YEAR, previous);
			previousYear.setText("< "+App.i18n.translate("comp.year.navigation.year.click", calendar.getTime()));
		} 
		else
		{
			previousYear.setText(" ");
		}
		
		if(index < years.size() - 1)
		{
			int next = years.get(index + 1);
			calendar.set(Calendar.YEAR, next);
			nextYear.setText(App.i18n.translate("comp.year.navigation.year.click", calendar.getTime())+" >");
		}
		else
		{
			nextYear.setText(" ");
		}
	}
	
	private enum YearNavigationType
	{
		PREVIOUS, NEXT;
	}
	
	private class YearNavigationListener extends MouseAdapter
	{
		private YearNavigationType type;
		
		private YearNavigationListener(YearNavigationType type)
		{
			this.type = type;
		}

		@Override
		public void mouseReleased(MouseEvent e)
		{
			log.trace(String.format("Year Navigation button clicked: %s", type));
			int year = YearNavigation.this.year;
			if(type==YearNavigationType.PREVIOUS)
			{
				year--;
			}
			else
			{
				year++;
			}
			App.events.fireYearSelected(new YearMonthSelectedEvent(this, user, year));
		}
	}
}
